package com.example.springboottools.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class WhiteBackgroundDetectorUtil {
    public static boolean isWhiteBackground(String imagePath) {
        try {
            BufferedImage image = ImageIO.read(new File(imagePath));
            int width = image.getWidth();
            int height = image.getHeight();

            for (int i = 0; i < width; i++) {
                for (int j = 0; j < height; j++) {
                    Color color = new Color(image.getRGB(i, j));
                    if (!isWhite(color)) {
                        return false;
                    }
                }
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    private static boolean isWhite(Color color) {
        // 设定一个阈值，小于该值则认为是白色
        int threshold = 500;
        return color.getRed() >= threshold && color.getGreen() >= threshold && color.getBlue() >= threshold;
    }

    public static void main(String[] args) {
        String imagePath = "C:\\Users\\SH102909\\Downloads\\cn7701023131EA_4_xnl.jpg";
        boolean isWhite = isWhiteBackground(imagePath);
        System.out.println("The image has a white background: " + isWhite);
    }
}
